/*************************************************
 *FileName:      site.js
 *Description:   我的收货地址.js
 *Others:        陈灿
 *************************************************/
    /*
     * 启动
     */
    mui.init();
    mui('.mui-scroll-wrapper').scroll({
        indicators: false,      //是否显示滚动条 默认为true
        deceleration: 0.003,    //阻尼系数,系数越小滑动越灵敏 默认0.0006
        bounce: true           //是否启用回弹 默认true
    });
    /*
     * 变量初始化
     */
    var uid = localStorage.replace_uid;
    var token = localStorage.replace_token;
    var orderid=getUrlParam('orderid');
    var orderopen=false;
    
    mui.plusReady(function(){ 
        var wv=plus.webview.currentWebview();// 关闭侧滑返回功能
        wv.setStyle({'popGesture':'none'});
        var old_back = mui.back;
        mui.back = function() {
            if(orderopen==true){
                old_back();
                orderopen=false;
            }else{
                mui.toast('请完成订单！', {
                    duration: 2000,
                    type: 'div'
                })
            }
        }
    });
    /*
     * 主程序
     */ 
    $(document).ready(function() 
    {
          bindTaps(['addsite']);
        //样式加载
        pagecss();
    });
    window.addEventListener('refreshsite', function(e){//执行刷新
      location.reload(); 
    });
    /**
     * pagecss 
     * 样式加载
     * @cc 
     * @DateTime 2018-01-08T06:19:16+0800
     * @param    int        
     * @return   void
     */
    function pagecss(){
        mui.plusReady(getlist)
        $('.site').on('tap','.delete',function(){
            var othis =$(this);
            mui.confirm('确定删除该地址？','提示',['确定','取消'],function(e){
                if(e.index==0){
                    delect(othis);
                }
            },'div')
        })
        
        $('.site').on('tap','a.lable',function(){
            $(this).addClass('active').parents('.con').siblings().find('a.lable').removeClass('active');
        })
        
        $('.site').on('tap','.editdata',function(){
            var id =$(this).parents('.con').attr('data-id');
            common.pageTransition('addsite.html?that='+id)
        })
    }


//获取信息
function getlist(){
    $.ajax({
        type: "post",
        url: DOMAIN_NAME + "/index.php/api/user/usersAddressList",
        dataType:'jsonp',
        data: { uid: uid, token: token},
        success: function(data) {
            if(data.error_code == 0){
                var ohtml ='';
                for(var i=0;i<data.data.length; i++){
                    var fst = data.data[i].default==1 ?'active' : '';
                    var sex =data.data[i].sex=='man' ? '男':'女';
                    ohtml +='<div class="con" data-id="'+data.data[i].id+'" ><p class="inf">'+data.data[i].name+'<span class="sex">'+sex+'</span><span class="phone">'+data.data[i].phone+'</span></p>'
                    +'<p class="site">'+data.data[i].province+data.data[i].city+data.data[i].district+data.data[i].address+'</p><p class="lable">'
                    +'<a class="lable '+fst+'"><em></em>选为收货地址</a><a class="delete">删除</a><a class="editdata">编辑</a></p></div>';
                }
                $('.main').html(ohtml);
            }else if(data.error_code == 1001){
                setTimeout(function(){
                        exitlogin();
                    },1000);
            }else{
            }
        }  
    });
}


function delect(othis){
    var id = othis.parents('.con').attr('data-id');
    $.ajax({
        type: "post",
        url: DOMAIN_NAME + "/index.php/api/user/addressDel",
        dataType:'jsonp',
        data: { uid: uid, token: token,id:id},
        success: function(data) {
            if(data.error_code == 0){
                othis.parents('.con').remove();
            }else if(data.error_code == 1001){
                setTimeout(function(){
                        exitlogin();
                    },1000);
            }else{
                mui.toast(data.error_msg)
            }
        }  
    });
}

$('.footer').on('tap',function(){
    var id = $('.lable.active').parents('.con').attr('data-id');
    $.ajax({
        type: "post",
        url: DOMAIN_NAME + "/index.php/api/order/orderAddress",
        dataType:'jsonp',
        data: { uid: uid, token: token,id:id,order_id:orderid},
        success: function(data) {
            if(data.error_code == 0){
                orderopen=true;
                mui.back();
                var list = plus.webview.currentWebview().opener();//触发父页面的自定义事件(refresh),从而进行刷新
                mui.fire(list, 'refreshmyprop');//返回true,继续页面关闭逻辑
                return true;
            }else if(data.error_code == 1001){
                setTimeout(function(){
                    exitlogin();
                },1000);
            }else{
                mui.toast(data.error_msg)
            }
        }  
    });
})
